<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/8/18
 * Time: 11:37
 */

namespace App\Http\Classes;
use Illuminate\Support\Facades\DB;

class UserLog
{
    private $tableName = 'user_log';
    private $tableDB = null;
    private $error = 0;
    private $message = array(
        0 => '修改密码失败',
        1 => '获取信息失败',
        3 => '邮箱注册失败',
        4 => '令牌过期',
        5 => '用户名与密码不匹配',
        6 => '旧密码不正确',
        7 => '修改信息失败',
        8 => '邮箱或密码错误'
    );

    public function __construct( $id = null )
    {
        $this->tableDB = DB::table( $this->tableName );

        if ( $id !== null )
        {
            $this->tableDB = $this->tableDB->where('user_id', '=', $id);
        }
    }

    public function getDaliyLog()
    {
        $log = $this->tableDB;

        $log = $log->select('daliy_study_time as time', 'daliy_words_count as words')->get();

        if ($log)
        {
            $log = $log[0];
            return $log;
        }
        else
        {
            $this->error = 1;
            return null;
        }
    }

    public function increaseDaliyTime($time)
    {
        $log = $this->tableDB;

        $log = $log->increment('daliy_study_time', $time);

        if ($log)
        {
            return true;
        }
        else
        {
            $this->error = 7;
            return false;
        }
    }

    public function increaseDaliyWord($count)
    {
        $log = $this->tableDB;

        $log = $log->increment('daliy_words_count', $count);

        if ( $log )
        {
            return true;
        }
        else
        {
            $this->error = 7;
            return false;
        }
    }

    public function getMessage()
    {
        return $this->message[ $this->error ];
    }
}